package com.lzy.user.controller;

import com.lzy.api.controller.user.BaseController;
import com.lzy.api.controller.user.HelloControllerApi;
import com.lzy.api.controller.user.MyFansControllerApi;
import com.lzy.enums.Sex;
import com.lzy.grace.result.GraceJSONResult;
import com.lzy.grace.result.ResponseStatusEnum;
import com.lzy.pojo.vo.FansCountsVO;
import com.lzy.pojo.vo.RegionRatioVO;
import com.lzy.user.service.MyFansService;
import com.lzy.utils.PagedGridResult;
import com.lzy.utils.RedisOperator;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class MyFansController extends BaseController implements MyFansControllerApi {

    @Autowired
    public MyFansService myFansService;

    @Override
    public GraceJSONResult isMeFollowThisWriter(String writerId, String fanId) {

        if(StringUtils.isBlank(writerId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_STATUS_ERROR);
        }

        boolean result = myFansService.isMeFollowThisWriter(writerId,fanId);
        return GraceJSONResult.ok(result);
    }

    @Override
    public GraceJSONResult follow(String writerId, String fanId) {

        if(StringUtils.isBlank(writerId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_STATUS_ERROR);
        }

        myFansService.follow(writerId, fanId);
        return GraceJSONResult.ok();
    }

    @Override
    public GraceJSONResult unfollow(String writerId, String fanId) {

        if(StringUtils.isBlank(writerId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_STATUS_ERROR);
        }

        myFansService.unfollow(writerId, fanId);
        return GraceJSONResult.ok();
    }


    @Override
    public GraceJSONResult queryAll(String writerId, Integer page, Integer pageSize) {

        if(StringUtils.isBlank(writerId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_STATUS_ERROR);
        }

        if (page == null) {
            page = COMMON_START_PAGE;
        }

        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult gridResult = myFansService.queryMyFansList(writerId, page, pageSize);
        return GraceJSONResult.ok(gridResult);
    }

    @Override
    public GraceJSONResult queryRatio(String writerId) {

        if(StringUtils.isBlank(writerId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_STATUS_ERROR);
        }

        // 查询男的多少人
        int manCounts = myFansService.queryFansCounts(writerId, Sex.man);
        // 查询女的多少人
        int womanCounts = myFansService.queryFansCounts(writerId, Sex.woman);

        FansCountsVO fansCountsVO = new FansCountsVO();
        fansCountsVO.setManCounts(manCounts);
        fansCountsVO.setWomanCounts(womanCounts);

        return GraceJSONResult.ok(fansCountsVO);
    }

    @Override
    public GraceJSONResult queryRatioByRegion(String writerId) {

        if(StringUtils.isBlank(writerId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_STATUS_ERROR);
        }


        List<RegionRatioVO> regionRatioVOList = myFansService.queryRatioByRegion(writerId);
        return GraceJSONResult.ok(regionRatioVOList);
    }

}

